include([
 'renderers.NodeRenderer'
 , 'shapes.Triangle'], 

function(){

	declare('renderers.TriangleRenderer');
	
	TriangleRenderer = NodeRenderer.extend({
		init : function(name){
			this._super(name || 'TriangleRenderer' + (Renderer.count++));
		},
		renderObject : function(gw, obj){
			var c = gw.c;
			var m = this.material;
			this.eachTriangle(obj, function(tri){
				m.render(gw, tri);
			});
			if( obj.children ){
				for(var e in obj.children ){
					if( obj.children[e] ) this.renderObject(gw, obj.children[e]);
				}
			}
			return true;
		},
		eachTriangle : function(obj, callback){
			if(obj.meshData){
				var data = obj.meshData();
				var len = data.length;
				var p1 = data[0];
				var y = 1 % len;
				var z = 2 % len;
				var triangle = new Triangle(p1,data[y],data[z]);
				if( callback(triangle) ){
					return;
				}
				while( z < len ){
					y = z;
					z++;
					
					triangle.set(0, p1);
					triangle.set(1, data[y]);
					triangle.set(2, data[z%len]);
					if( callback(triangle) )
						return;
				}
			}
		}
	});

});